package com.ruoyi.web.controller.system.city;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.system.domain.city.CityCarousel;
import com.ruoyi.system.domain.city.CityModule;
import com.ruoyi.web.core.config.OssConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.city.CityProject;
import com.ruoyi.system.service.city.ICityProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.util.HtmlUtils;

/**
 * 项目Controller
 * 
 * @author cjq
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/system/city/project")
public class CityProjectController extends BaseController
{
    @Autowired
    private ICityProjectService cityProjectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('city:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityProject cityProject)
    {
        startPage();
        List<CityProject> list = cityProjectService.selectCityProjectList(cityProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('city:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityProject cityProject)
    {
        List<CityProject> list = cityProjectService.selectCityProjectList(cityProject);
        List<CityProject> imageList=new ArrayList<>();

        if (!list.isEmpty()){
            for (CityProject m:list) {
                m.setProjectUrl(OssConfig.IMAGE_URL+m.getProjectUrl());
                imageList.add(m);
            }
        }
        ExcelUtil<CityProject> util = new ExcelUtil<CityProject>(CityProject.class);
        util.exportExcel(response, imageList, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(cityProjectService.selectCityProjectByProjectId(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('city:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityProject cityProject)
    {
        cityProject.setProjectId(IdUtil.getSnowflakeNextId());
        return toAjax(cityProjectService.insertCityProject(cityProject));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('city:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityProject cityProject)
    {
        System.out.println("cityProject:"+cityProject);
//        cityProject.setProjectContent(HtmlUtils.htmlUnescape(cityProject.getProjectContent()));
        return toAjax(cityProjectService.updateCityProject(cityProject));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('city:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(cityProjectService.deleteCityProjectByProjectIds(projectIds));
    }
}
